//
//  TopicTableViewCell.m
//  NewChat
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "TopicTableViewCell.h"



//定义宏，用来cell布局
//#define kWid self.contentView.frame.size.width
//#define kHei self.contentView.frame.size.height
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@implementation TopicTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
        [self addAllViews];
    }
    return self;
}

#pragma mark---------添加子控件-------------
-(void)addAllViews{
    
    [self addSubview:self.userPictureBtn];
    [self addSubview:self.nickNameLbl];
    [self addSubview:self.timeLbl];
    [self addSubview:self.topicLbl];
    [self addSubview:self.messageLbl];
    [self addSubview:self.voiceBtn];
    
}

#pragma mark----------cell自适应------
/**
 *  自定义的方法，cell根据lable自适应（使用这个方法不需要重写layoutSubviews方法）
 *
 *  @param model cell中子控件需要的数据
 */

 - (void)configureCellWithModel:(FindModel *)model{
     //头像
     _userPictureBtn.frame = CGRectMake(kWidth*0.03, kWidth*0.03, kWidth*0.18, kWidth*0.18);
     [self.userPictureBtn setBackgroundImage:[UIImage imageNamed:model.owner[@"userImageName"]] forState:(UIControlStateNormal)];

     //设置圆角
     _userPictureBtn.layer.cornerRadius = kWidth*0.09;
     _userPictureBtn.layer.masksToBounds = YES;
     
     //昵称
     _nickNameLbl.frame = CGRectMake(kWidth*0.22, kWidth*0.03, kWidth*0.5, kWidth*0.08);
     self.nickNameLbl.text = model.owner[@"nickname"];
     self.nickNameLbl.font = [UIFont systemFontOfSize:20];
     
     //发布时间
     _timeLbl.frame = CGRectMake(kWidth*0.22, kWidth*0.12, kWidth*0.3, kWidth*0.08);
     self.timeLbl.text = model.startTime;
    
     //话题
     _topicLbl.numberOfLines = 0;
     _topicLbl.lineBreakMode = NSLineBreakByWordWrapping;
     self.topicLbl.text = model.topicMessage;
     CGSize size = [self.topicLbl sizeThatFits:CGSizeMake(kWidth*0.9, MAXFLOAT)];
     _topicLbl.frame = CGRectMake(kWidth*0.05, kWidth*0.23, kWidth*0.9, size.height);
     
     //回复消息
     self.messageLbl.frame = CGRectMake(kWidth*0.75, kWidth*0.03, kWidth*0.2, kWidth*0.08);
     self.messageLbl.text = [NSString stringWithFormat:@"%ld回复",model.replys];
     
    //语音消息
     self.voiceBtn.frame = CGRectMake(kWidth*0.05, CGRectGetMaxY(self.topicLbl.frame)+10, kWidth*0.6, kWidth*0.1);
     //设置边框
     self.voiceBtn.layer.borderWidth = 1;

     //  设置voiceBtn上的文字距离左侧140个像素
     [self.voiceBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 140, 0, 0)];
     
     //cell的高度根据lable适应
     self.frame = CGRectMake(0,0, kWidth, CGRectGetMaxY(self.topicLbl.frame)+CGRectGetHeight(self.voiceBtn.frame)+20);
 
 }




#pragma mark----------懒加载子控件----------

-(UIButton *)userPictureBtn{
    
    if (!_userPictureBtn) {
        
        _userPictureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _userPictureBtn.frame = CGRectZero;
        _userPictureBtn.backgroundColor = [UIColor clearColor];

    }
    return _userPictureBtn;
}



-(UILabel *)nickNameLbl{
    if (!_nickNameLbl) {
        
        _nickNameLbl = [[UILabel alloc]init];
        _nickNameLbl.frame = CGRectZero;
        _nickNameLbl.backgroundColor = [UIColor clearColor];
    }
    return _nickNameLbl;
    
}

-(UILabel *)timeLbl{
    if (!_timeLbl) {
        _timeLbl = [[UILabel alloc]init];
        _timeLbl.frame = CGRectZero;
        _timeLbl.backgroundColor = [UIColor clearColor];
    }
    return _timeLbl;
    
}

-(UILabel *)topicLbl{
    if (!_topicLbl) {
        _topicLbl = [[UILabel alloc]init];
        _topicLbl.frame = CGRectZero;
        _topicLbl.backgroundColor =[UIColor clearColor];
        
    }
    return _topicLbl;
}

-(UILabel *)messageLbl{
    
    if (!_messageLbl) {
        _messageLbl = [[UILabel alloc]init];
        _messageLbl.frame = CGRectZero;
        _messageLbl.backgroundColor = [UIColor clearColor];

        
    }
    return _messageLbl;
    
    
}


-(UIButton *)voiceBtn{
    if (!_voiceBtn) {
        _voiceBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _voiceBtn.frame = CGRectZero;
        _voiceBtn.backgroundColor = [UIColor clearColor];
        _voiceBtn.layer.cornerRadius = 20;
    }
    return _voiceBtn;
}


//-(void)clickVoiceBtnAction:(UIButton*)sender{
//    
//    NSLog(@"点击了TopicTableViewCell的子控件——>VoiceBtn");
//    NSLog(@"这个控件的点击事件在TopicTableViewCell.m中");
//    NSLog(@"还没有写播放语音的功能");
//    
//    
//    
//    
//}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
